package com.rpay.service.basic.impl;

import com.rpay.common.dto.basic.BasicDTO;
import com.rpay.common.dto.basic.BasicQueryDTO;
import com.rpay.dao.basic.BasicDao;
import com.rpay.model.basic.BasicDO;
import com.rpay.service.basic.IBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XXX
 * @mail XXX@hotmail.com
 * @date 2018/5/13
 * @Description:
 */
@Service
public class BasicServiceImpl implements IBasicService {
    @Autowired
    BasicDao basicDao;

    @Override
    public BasicQueryDTO getDataList() {
        List<BasicDO> basicDOList = basicDao.selectDataList();
        List<BasicDTO> list = new ArrayList<>();
        if (basicDOList != null && basicDOList.size() > 0) {
            for (BasicDO basicDO : basicDOList) {
                BasicDTO basicDTO = convertDOToDTO(basicDO);
                list.add(basicDTO);
            }
        }
        BasicQueryDTO data = convertSqlDOToDTO(list);
        return data;
    }

    @Override
    public BasicDTO findByCode(String code) {
        BasicDO basicDO = basicDao.findByCode(code);
        BasicDTO info = convertDOToDTO(basicDO);
        return info;
    }

    @Override
    public boolean updateBasic(BasicDTO basicDTO) {
        BasicDO basicDO = convertDTOtoDO(basicDTO);
        basicDao.updateByCode(basicDO);
        return true;
    }

    private BasicDTO convertDOToDTO(BasicDO basicDO) {
        BasicDTO basicDTO = null;
        if (basicDO != null) {
            basicDTO = new BasicDTO();
            basicDTO.setCode(basicDO.getCode());
            basicDTO.setName(basicDO.getName());
            basicDTO.setEnName(basicDO.getEnName());
            basicDTO.setContent(basicDO.getContent());
            basicDTO.setEnContent(basicDO.getEnContent());
            basicDTO.setCreateTime(basicDO.getCreateTime());
            basicDTO.setUpdateTime(basicDO.getUpdateTime());
        }
        return basicDTO;
    }

    private BasicDO convertDTOtoDO(BasicDTO basicDTO) {
        BasicDO basicDO = null;
        if (basicDTO != null) {
            basicDO = new BasicDO();
            basicDO.setCode(basicDTO.getCode());
            basicDO.setName(basicDTO.getName());
            basicDO.setEnName(basicDTO.getEnName());
            basicDO.setContent(basicDTO.getContent());
            basicDO.setEnContent(basicDTO.getEnContent());
        }
        return basicDO;
    }

    private BasicQueryDTO convertSqlDOToDTO(List list) {
        BasicQueryDTO basicQueryDTO = null;
        basicQueryDTO = new BasicQueryDTO();
        basicQueryDTO.setList(list);
        return basicQueryDTO;
    }
}
